package com.etogc.groupchat.group.frgm;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.etogc.groupchat.R;
import com.etogc.groupchat.base.BaseFragment;
import com.etogc.groupchat.group.adpt.ConversationAdapter;
import com.etogc.groupchat.group.ui.SearchActivity;
import com.etogc.util.IntentHelper;

import io.rong.imkit.MainActivity;
import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.CSCustomServiceInfo;
import io.rong.imlib.model.Conversation;

/**
 * Created by AMing on 16/6/21.
 * Company RongCloud
 */
public class GroupFragment extends BaseFragment implements View.OnClickListener {

    private ConversationListFragment conversationFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frgm_chat, container, false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        view.findViewById(R.id.llSearch).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentHelper.jump(getActivity(), SearchActivity.class);
            }
        });
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        ConversationAdapter conversationAdapter = new ConversationAdapter(getActivity());
        if (conversationFragment != null) {
            fragmentTransaction.remove(conversationFragment);
        }
        conversationFragment = new ConversationListFragment();
        conversationFragment.setAdapter(conversationAdapter);
        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")
                .build();
        conversationFragment.setUri(uri);
        fragmentTransaction.add(R.id.container1, conversationFragment).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_user_profile:
                startActivity(new Intent(getActivity(), MainActivity.class));
                break;
            case R.id.mine_service:
                CSCustomServiceInfo.Builder builder = new CSCustomServiceInfo.Builder();
                builder.province("北京");
                builder.city("北京");
                RongIM.getInstance().startCustomerServiceChat(getActivity(), "KEFU146001495753714", "在线客服", builder.build());
                // KEFU146001495753714 正式  KEFU145930951497220 测试  小能: zf_1000_1481459114694   zf_1000_1480591492399
                break;
            case R.id.my_wallet:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
